﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PDFLibNet;

namespace kiosk
{
    public partial class DataDisplayOption : Form
    {
        private DataBaseHandler handler;
        private Int32 dataID = -1;
        public DataDisplayOption(Int32 dataID)
        {
            InitializeComponent();
            this.dataID = dataID;

            //
            this.handler = new DataBaseHandler();
            this.saveBtn.Click += new EventHandler(saveBtn_Click);
            this.exitBtn.Click +=new EventHandler(exitBtn_Click);

            //

        }


        private void fillCombobBox()
        {


            DisplayOption[] options = new DisplayOption[7];

            options[0] = new DisplayOption();
            options[0].dText = "Default";
            options[0].dataOption = (Int32)RotateView.Default;

            options[1] = new DisplayOption();
            options[1].dText = "RotateBy90";
            options[1].dataOption = (Int32)RotateView.RotateBy90;

            options[2] = new DisplayOption();
            options[2].dText = "RotateBy180";
            options[2].dataOption = (Int32)RotateView.RotateBy180;

            options[3] = new DisplayOption();
            options[3].dText = "RotateBy270";
            options[3].dataOption = (Int32)RotateView.RotateBy270;

            options[4] = new DisplayOption();
            options[4].dText = "RotateByFlipx";
            options[4].dataOption = (Int32)RotateView.RotateByFlipx;

            options[5] = new DisplayOption();
            options[5].dText = "RotateByFlipy";
            options[5].dataOption = (Int32)RotateView.RotateByFlipy;

            options[6] = new DisplayOption();
            options[6].dText = "RotateFlipxy";
            options[6].dataOption = (Int32)RotateView.RotateFlipxy;
            this.rotateCbo.Items.AddRange(options);





        }

        // function that gets the datas from the database and shows to the user via the window
        private void setupCboandText()
        {
            string text;
            try
            {
                Byte dataarray = (Byte)this.handler.getDataOpt(this.dataID);
                text = handler.getDataHeading(this.dataID);

                if ((dataarray & (Byte)RotateView.Default) == (Byte)RotateView.Default)
                {
                    // The default view is set
                    this.rotateCbo.SelectedIndex = 0;


                }

                else if ((dataarray & (Byte)RotateView.RotateBy90) == (Byte)RotateView.RotateBy90)
                {

                    // rotate by 90 set
                    this.rotateCbo.SelectedIndex = 1;

                }

                else if ((dataarray & (Byte)RotateView.RotateBy180) == (Byte)RotateView.RotateBy180)
                {
                    // rotate by 180 set
                    this.rotateCbo.SelectedIndex = 2;


                }

                else if ((dataarray & (Byte)RotateView.RotateBy270) == (Byte)RotateView.RotateBy270)
                {
                    // rotate by 270 is set
                    this.rotateCbo.SelectedIndex = 3;


                }

                else if ((dataarray & (Byte)RotateView.RotateByFlipx) == (Byte)RotateView.RotateByFlipx)
                {
                    // rotate by flip x is selected
                    this.rotateCbo.SelectedIndex = 4;

                }

                else if ((dataarray & (Byte)RotateView.RotateByFlipy) == (Byte)RotateView.RotateByFlipy)
                {
                    // rota by y is selected 
                    this.rotateCbo.SelectedIndex = 5;

                }

                else if ((dataarray & (Byte)RotateView.RotateFlipxy) == (Byte)RotateView.RotateFlipxy)
                {
                    // Rotate by xy is selected
                    this.rotateCbo.SelectedIndex = 6;


                }






                this.headingTextBox.Text = text;

            }

            catch (Exception ex)
            {

                MessageBox.Show("There was an error" + ex.Message);

            }


        }

        private void DataDisplayOption_Load(object sender, EventArgs e)
        {

            // Have to load the onfig from the database]
            //

            this.fillCombobBox();

            this.setupCboandText();



        }

        private void saveBtn_Click(object sender, EventArgs e)
        {


            if (this.rotateCbo.SelectedItem.ToString() == String.Empty)
            {
                MessageBox.Show("please selected a item from the downlist (Combo box)");
                return;
            }


            else if (this.dataID == -1)
            {
                MessageBox.Show("Application error in the system");
                return;

            }
            
            if(this.headingTextBox.Text.Trim().Length > 100 )
            {
                MessageBox.Show("The heading must be less then 100 characters");
                return;
            
            }
            
            DisplayOption cboSelected = (DisplayOption)this.rotateCbo.SelectedItem;
            string headingText = this.headingTextBox.Text;

            //Build up the options n the indow of the  systems
            Byte option = Convert.ToByte(cboSelected.dataOption);

            try
            {

                handler.setDataOpt(this.dataID, option);
                handler.updateHeading(this.dataID, headingText);
                MessageBox.Show(" Updated Configuration Saved (This Window will close)");
                GeneralOpt.pdfHeadingChanged = true;
                this.Close();

            
            
            
            }

            catch (Exception ex)
            {
                MessageBox.Show("Please contract admin a error has occured in the system"+ex.Message);

            }

        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rndVBtn_Click(object sender, EventArgs e)
        {


            if (MessageBox.Show("This Will Delete the Previoud Data View and upload with a new data !! Continue ?? ", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {

                // the logic for the load pdf button
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Portable Document Format (*.pdf)|*.pdf";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    
                    // update the current data
                    try
                    {
                        this.handler.updateTxtData(this.dataID, dlg.FileName);
                        MessageBox.Show("Data saved !! ");
                        MessageBox.Show("Inorder to reflect new data view please close the data view and then open again.  \n This will close now");
                        GeneralOpt.pdfDataChanged = true;
                        this.Close();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show("There has been some error "+ ex.Message);
                    
                    }

                }


            }



        }

      
       
    }
}
